package cn.wolfcode.wolf2w.business.listener;

import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.service.IStrategyService;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Stack;

/**
 * @author : 邓风来
 * @date : 2025/9/22 11:38
 */

/**
 * ApplicationListener:程序监听器
 * ApplicationReadyEvent:监听和层序准备完成后的时机点
 */
@Component
public class ApplicationInitListener implements ApplicationListener<ApplicationReadyEvent> {


    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private RedisService redisService;
//    @Override
//    public void onApplicationEvent(ApplicationReadyEvent event) {
//        System.err.println("应用程序准备完成");
//        for (Strategy strategy : strategyService.list()) {
//            String key = RedisKeys.STRATEGY_STATIS_HASH.join(strategy.getId().toString());
//            if (redisService.hasKey(key)){
//                continue;
//            }
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("viewnum", Integer.valueOf(strategy.getViewnum().toString()));
//            map.put("replynum", Integer.valueOf(strategy.getReplynum().toString()));
//            map.put("sharenum", Integer.valueOf(strategy.getSharenum().toString()));
//            map.put("favornum", Integer.valueOf(strategy.getFavornum().toString()));
//            map.put("thumbsupnum", Integer.valueOf(strategy.getThumbsupnum().toString()));
//            map.put("id",Integer.valueOf(strategy.getId().toString()));
//            redisService.setCacheMap(key,map);
//
//
//
//
//        }
//
//    }
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
    System.err.println("应用程序准备完成");
    for (Strategy strategy : strategyService.list()) {
        String key = RedisKeys.STRATEGY_STATIS_HASH.join(strategy.getId().toString());
        if (redisService.hasKey(key)) {
            continue;
        }
        HashMap<String, Object> map = new HashMap<>();

        // 为每个可能为null的字段添加空值检查
        map.put("viewnum", Integer.valueOf(strategy.getViewnum() != null ? strategy.getViewnum().toString() : "0"));
        map.put("replynum", Integer.valueOf(strategy.getReplynum() != null ? strategy.getReplynum().toString() : "0"));
        map.put("sharenum", Integer.valueOf(strategy.getSharenum() != null ? strategy.getSharenum().toString() : "0"));
        map.put("favornum", Integer.valueOf(strategy.getFavornum() != null ? strategy.getFavornum().toString() : "0"));
        map.put("thumbsupnum", Integer.valueOf(strategy.getThumbsupnum() != null ? strategy.getThumbsupnum().toString() : "0"));
        map.put("id", Integer.valueOf(strategy.getId().toString()));

        redisService.setCacheMap(key, map);
    }
}
}
